/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
 class Solution {
    public:
        vector<int> largestValues(TreeNode* root) {
            queue<TreeNode*> q;
            vector<int> ret;
            if (root == nullptr)
                return ret;
            q.push(root);
    
            while (!q.empty()) {
                int sz = q.size();
                int tmp = INT_MIN;
                for (int i = 0; i < sz; i++) {
                    auto q1 = q.front();
                    q.pop();
                    tmp = max(tmp, q1->val);
                    if (q1->left)
                        q.push(q1->left);
                    if (q1->right)
                        q.push(q1->right);
                }
                ret.push_back(tmp);
            }
            return ret;
        }
    };
    /**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    public:
        int widthOfBinaryTree(TreeNode* root) {
            vector<pair<TreeNode*,unsigned int>> q;
            q.push_back({root,1});
            unsigned int ret =0;
            while(q.size())
            {
                auto&[x1,y1]=q[0];
                auto&[x2,y2]=q.back();
                ret = max(ret,y2-y1+1);
                vector<pair<TreeNode*,unsigned int>> tmp;
                for(auto&[x,y]:q)
                {
                    if(x->left) tmp.push_back({x->left,y*2});
                    if(x->right) tmp.push_back({x->right,y*2+1});
                }
                q = tmp;
            }
            return ret;
        }
    };